package com.outfood.service.impl;

import com.outfood.config.WxConfig;
import com.outfood.dto.LoginDTO;
import com.outfood.intercept.LoginIntercept;
import com.outfood.service.IUserService;
import com.outfood.vo.LoginVO;
import com.rys.common.utils.Search;
import com.tps.open.weixin.dto.GenerateOpenIdDTO;
import com.tps.open.weixin.open.GenerateOpenIdService;
import com.tps.open.weixin.vo.Jscode2SessionVO;
import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;

import com.rys.framework.service.base.BaseServcie;
import com.outfood.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Log4j2
public class UserServiceImpl extends BaseServcie<User> implements IUserService {

	 @Reference
	 GenerateOpenIdService generateOpenIdService;
	 @Autowired
     WxConfig wxConfig;
	 @Resource
	 RedisTemplate redisTemplate;
//
	 //小程序登录
	 @Override
	 public LoginVO login(LoginDTO loginDTO) {

		 GenerateOpenIdDTO generateOpenIdDTO = new GenerateOpenIdDTO();
		 generateOpenIdDTO.setCode(loginDTO.getCode());
		 generateOpenIdDTO.setAppid(wxConfig.getWxAppid());
		 generateOpenIdDTO.setAppSecret(wxConfig.getWxSecret());
		 Jscode2SessionVO jscode2SessionVO = generateOpenIdService.GenerateOpenId(generateOpenIdDTO);

		 List<User> list = super.getList(new Search().add("openId", jscode2SessionVO.getOpenid()));
		 LoginVO loginVO = new LoginVO();
		 User user = new User();
		 if(list.size() > 0) {
			 user = list.get(0);
			 loginVO.setHeadImg(user.getHeadImg());
			 loginVO.setNickName(user.getNickName());
		 }else {
			 user.setOpenId(jscode2SessionVO.getOpenid());
			 super.add(user,user.getOpenId());
			 loginVO.setHeadImg(user.getHeadImg());
			 loginVO.setNickName(user.getNickName());
		 }

		 String token = UUID.randomUUID().toString();
		 redisTemplate.opsForValue().set(LoginIntercept.LOGINEDUSER+token,user.getId(),2, TimeUnit.DAYS);
		 loginVO.setUserId(user.getId());
		 loginVO.setToken(token);
		 return loginVO;
	 }















//
//
//	 private Integer getUserStatus(User user) {
//		 Integer userStatus = 0;
//		 if(user.getNickName() != null && user.getMobile() == null) {
//			 userStatus = 1;
//		 }
//		 if(user.getNickName() != null && user.getMobile() != null) {
//			 userStatus = 2;
//		 }
//		 return userStatus;
//	 }
//
//	 @Reference
//	 XcxDecryptUserInfoService decryptUserInfoService;
//
//	 //获取用户信息
//	 @Override
//	 public UserInfoVO getUserInfo(UserInfoDTO userInfoDTO) throws Exception {
//
//		 DecryptUserInfoDTO decryptUserInfoDTO = new DecryptUserInfoDTO();
//		 User user = super.get(userInfoDTO.getUserId());
//		 decryptUserInfoDTO.setOpenId(user.getOpenId());
//		 decryptUserInfoDTO.setIv(userInfoDTO.getIv());
//		 decryptUserInfoDTO.setEncryptedData(userInfoDTO.getEncryptedData());
//		 DecryptUserInfoVO decryptUserInfo = decryptUserInfoService.decryptUserInfo(decryptUserInfoDTO);
//		 user.setNickName(decryptUserInfo.getNickName());
//		 user.setHeadImage(decryptUserInfo.getAvatarUrl());
//		 user.setSex(decryptUserInfo.getGender());
//		 super.update(user.getId(), user, user.getId().toString());
//		 UserInfoVO userInfoVO = new UserInfoVO();
//		 userInfoVO.setHeadImage(decryptUserInfo.getAvatarUrl());
//		 userInfoVO.setNickName(decryptUserInfo.getNickName());
//		 return userInfoVO;
//
//	 }
//
//	 @Reference
//	 XcxDecryptedPhoneNumberService decryptedPhoneNumberService;
//
//	 //获取用户手机号
//	 @Override
//	 public ResponseMobileVO getMobile(RequestMobileDTO requestMobileDTO) {
//
//		 PhoneNumberDTO phoneNumberDTO = new PhoneNumberDTO();
//		 phoneNumberDTO.setEncryptedData(requestMobileDTO.getEncryptedData());
//		 phoneNumberDTO.setIv(requestMobileDTO.getIv());
//		 User user = super.get(requestMobileDTO.getUserId());
//		 phoneNumberDTO.setOpenId(user.getOpenId());
//		 PhoneNumberVO decryptedPhoneNumber = null;
//		 try {
//			 decryptedPhoneNumber = decryptedPhoneNumberService.decryptedPhoneNumber(phoneNumberDTO);
//		 } catch (Exception e) {
//			 log.error(e.getMessage(),e);
//		 }
//		ResponseMobileVO responseMobileVO = new ResponseMobileVO();
//		responseMobileVO.setMobile(decryptedPhoneNumber.getPhoneNumber());
//		user.setMobile(decryptedPhoneNumber.getPhoneNumber());
//		super.update(user.getId(), user, user.getId().toString());
//		return responseMobileVO;
//
//
//	 }
//
//
//	 public void modifyPosition(ModifyPositionDTO modifyPositionDTO) {
//
//		 User user = super.get(modifyPositionDTO.getUserId());
//		 user.setLon(modifyPositionDTO.getLon());
//		 user.setLat(modifyPositionDTO.getLat());
//		 super.update(user.getId(), user, user.getId().toString());
//
//	 }
	 


	 
//	 public GetDriverVO getDriver(Double lat,Double lon,Long driverId) {
//		 GetDriverDTO getDriverDTO = new GetDriverDTO();
//		 getDriverDTO.setLat(lat);
//		 getDriverDTO.setLon(lon);
//		 OilSite oilSite = oilSiteService.getOilSite(getDriverDTO);		 
//		 List<User> list = super.getList(new Search().add("siteId", oilSite.getId()).add("idetify", "2").add("id", driverId));	 
//		 int nextInt = new Random().nextInt(list.size());
//		 User user = list.get(nextInt);
//		 GetDriverVO getDriverVO = new GetDriverVO();
//		 getDriverVO.setDriverMobile(user.getMobile());
//		 getDriverVO.setDriverName(user.getNickName());
//		 getDriverVO.setDriverId(user.getId());
//		 getDriverVO.setHeadImg(user.getHeadImage());
//		 getDriverVO.setDistance(oilSite.getDistance());
//		 getDriverVO.setLat(oilSite.getLat());
//		 getDriverVO.setLon(oilSite.getLon());
//		 return getDriverVO;
//	 }
	 
	 
}
